package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.mysql.MysqlLink;
import com.ruoyi.system.domain.DabaTable;
import com.ruoyi.system.domain.DatabaseDetails;
import com.ruoyi.system.service.IDabaTableService;
import com.ruoyi.system.service.IDatabaseDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 数据库Controller
 *
 * @author jw
 * @date 2022-06-13
 */
@Controller
@RequestMapping("/system/details")
public class DatabaseDetailsController extends BaseController
{

    @Autowired
    private IDatabaseDetailsService databaseDetailsService;
    @Autowired
    private IDabaTableService dabaTableService;

    private String prefix = "system/details";


    @GetMapping()
    public String details()
    {
        return prefix + "/details";
    }

    /**
     * 查询数据库列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DatabaseDetails databaseDetails)
    {
        startPage();
        List<DatabaseDetails> list = databaseDetailsService.selectDatabaseDetailsList(databaseDetails);
        return getDataTable(list);
    }


    /**
     * 新增数据库
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据库
     */
    @Log(title = "数据库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DatabaseDetails databaseDetails)
    {
        return toAjax(databaseDetailsService.insertDatabaseDetails(databaseDetails));
    }

    /**
     * 修改数据库
     */
    @GetMapping("/edit/{databaseId}")
    public String edit(@PathVariable("databaseId") Long databaseId, ModelMap mmap)
    {
        DatabaseDetails databaseDetails = databaseDetailsService.selectDatabaseDetailsByDatabaseId(databaseId);
        mmap.put("databaseDetails", databaseDetails);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据库
     */
    @Log(title = "数据库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DatabaseDetails databaseDetails)
    {
        return toAjax(databaseDetailsService.updateDatabaseDetails(databaseDetails));
    }

    /**
     * 删除数据库
     */
    @Log(title = "数据库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(databaseDetailsService.deleteDatabaseDetailsByDatabaseIds(ids));
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkIpUnique")
    @ResponseBody
    public String checkIpUnique(DatabaseDetails databaseDetails)
    {
        return databaseDetailsService.checkIpUnique(databaseDetails);
    }
}
